package com.sub.framework.rabbitmq;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sub.common.proto.DataProto;
import com.sub.framework.rabbitmq.message.ProtoMessageConverter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.nio.ByteBuffer;
import java.util.*;

/**
 *  基本消息模型 - 生产者
 */
//@Component
public class BasciPublisher {

    private static final Logger loger = LoggerFactory.getLogger(BasciPublisher.class);

    /** JSON 序列化和反序列化*/
    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /** 定义环境变量读取*/
    @Autowired
    private Environment environment;

    public void sendMsg(byte[] message){
//        if (! Strings.isNullOrEmpty(message)) {
            try {
                /*定义消息传输的格式*/
//                rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
                rabbitTemplate.setMessageConverter(new ProtoMessageConverter());
                rabbitTemplate.setExchange("amq.topic");
//                rabbitTemplate.setExchange("wootop.monitor.topic");
                rabbitTemplate.setRoutingKey("wootop.ecg.1951"); ;
                MessageProperties messageProperties =  new MessageProperties();
//                DataProto.Content contentData = DataProto.Content.parseFrom(message);
//                Message msg = MessageBuilder.withBody(ByteBuffer.wrap(message, 0, message.length).array()).build();
//                Message msg = MessageBuilder.withBody(message).build();
                messageProperties.setContentType("application/octet-stream");
                messageProperties.setContentEncoding("UTF-8");

                List<Integer> ecgRawData  = new ArrayList<>();
                ecgRawData.add(1234);
                DataProto.Content.Builder builder = DataProto.Content.newBuilder();
                builder.addAllData(ecgRawData); //暂按30秒数据，后台可配置
                builder.setNum(1);
                builder.setRemark("12312"); //蓝牙重连更新
                builder.setSequence(1);  //蓝牙重连 重置1开始
                builder.setType("ECG");
                builder.setId(123423);
                builder.setText("开始时间戳|结束时间戳");
                builder.setCrc("CRC16");
//                Message msg = new Message(, messageProperties);

                Map<String ,Integer> mp = new HashMap<>();
                mp.put("1",1);
                mp.put("z",2);
                mp.put("x",3);
                rabbitTemplate.convertAndSend(builder.build());
//                rabbitTemplate.convertAndSend(mp);
//                System.out.println("基本消息模型-生产者-发送消息："+message);
//                loger.info("基本消息模型-生产者-发送消息：{}",message);

            }catch (Exception e){
                loger.info("基本消息模型-生产者-发送消息异常：{}",message,e.fillInStackTrace());
            }
//        }
    }


    private static ByteBuffer byte2Buffer(byte[] value) {
//	    ByteBuffer byteBuffer = ByteBuffer.allocate(value.length);
//	    byteBuffer.clear();
//	    byteBuffer.get(value, 0, value.length);

        return ByteBuffer.wrap(value, 0, value.length);
    }
}
